// 模拟数据库 - 工单数据管理
class OrderDataManager {
  constructor() {
    // 模拟数据库存储
    this.orders = [
      {
        id: 1,
        title: '灯管维修',
        color: '#7ED321',
        status: 'done',
        statusText: '已完成',
        statusClass: 'status-done',
        time: '2025-6-6 11:18:44',
        reporter: '张老师',
        phone: '139****5678',
        location: '教学楼B座306',
        detail: '灯管有3只不亮，2只闪烁',
        type: '维修部李维修',
        finishTime: '2025-6-6 15:30:22',
        comment: {
          user: '张老师',
          date: '2025年6月6日',
          content: '维修及时，服务态度很好！'
        }
      },
      {
        id: 2,
        title: '桌椅维修',
        color: '#F5C242',
        status: 'pending',
        statusText: '待处理',
        statusClass: 'status-pending',
        time: '2025-6-6 11:18:44',
        reporter: '李老师',
        phone: '139****1234',
        location: '教学楼E座426',
        detail: '桌椅损坏，需要更换',
        type: '维修部李维修',
        finishTime: '2025-6-7 18:00:00',
        expectedTime: '2025-6-7 18:00:00',
        comment: null
      },
      {
        id: 3,  
        title: '水电维修',
        color: '#4095E5',
        status: 'processing',
        statusText: '处理中',
        statusClass: 'status-processing',
        time: '2025-6-6 11:18:44',
        reporter: '吕同学',
        phone: '137****9876',
        location: '学生宿舍5公寓201',
        detail: '水龙头漏水严重，影响正常使用',
        type: '维修部李维修',
        startTime: '2025-6-6 14:15:00',
        expectedTime: '2025-6-6 15:00:00',
        comment: null
      },
      {
        id: 4,
        title: '门锁故障',
        color: '#FF5A5F',
        status: 'overtime',
        statusText: '已超时',
        statusClass: 'status-overdue',
        time: '2025-6-6 11:18:44',
        reporter: '赵老师',
        phone: '136****5432',
        location: '图书馆C座',
        detail: '电子门锁无法识别门卡',
        type: '维修部李维修',
        expectedTime: '2025-6-6 13:30:00',
        comment: null,
        isOverdue: true
      }
    ];
  }

  // 获取所有工单
  getAllOrders() {
    return this.orders;
  }

  // 根据ID获取工单
  getOrderById(id) {
    return this.orders.find(order => order.id === id);
  }

  // 根据状态获取工单
  getOrdersByStatus(status) {
    return this.orders.filter(order => order.status === status);
  }

  // 添加新工单
  addOrder(orderData) {
    const newOrder = {
      id: this.generateId(),
      ...orderData,
      time: new Date().toLocaleString(),
      status: 'pending',
      statusText: '待处理',
      statusClass: 'status-pending'
    };
    this.orders.push(newOrder);
    return newOrder;
  }

  // 更新工单状态
  updateOrderStatus(id, status) {
    const order = this.orders.find(o => o.id === id);
    if (order) {
      order.status = status;
      switch (status) {
        case 'pending':
          order.statusText = '待处理';
          order.statusClass = 'status-pending';
          break;
        case 'processing':
          order.statusText = '处理中';
          order.statusClass = 'status-processing';
          order.startTime = new Date().toLocaleString();
          break;
        case 'done':
          order.statusText = '已完成';
          order.statusClass = 'status-done';
          order.finishTime = new Date().toLocaleString();
          break;
        case 'overtime':
          order.statusText = '已超时';
          order.statusClass = 'status-overdue';
          order.isOverdue = true;
          break;
      }
      return order;
    }
    return null;
  }

  // 更新工单信息
  updateOrder(id, updateData) {
    const order = this.orders.find(o => o.id === id);
    if (order) {
      Object.assign(order, updateData);
      return order;
    }
    return null;
  }

  // 删除工单
  deleteOrder(id) {
    const index = this.orders.findIndex(o => o.id === id);
    if (index !== -1) {
      this.orders.splice(index, 1);
      return true;
    }
    return false;
  }

  // 生成新ID
  generateId() {
    return Math.max(...this.orders.map(o => o.id)) + 1;
  }

  // 搜索工单
  searchOrders(keyword) {
    return this.orders.filter(order => 
      order.title.includes(keyword) ||
      order.reporter.includes(keyword) ||
      order.phone.includes(keyword) ||
      order.location.includes(keyword) ||
      order.detail.includes(keyword)
    );
  }

  // 获取统计数据
  getStatistics() {
    const total = this.orders.length;
    const pending = this.orders.filter(o => o.status === 'pending').length;
    const processing = this.orders.filter(o => o.status === 'processing').length;
    const done = this.orders.filter(o => o.status === 'done').length;
    const overtime = this.orders.filter(o => o.status === 'overtime').length;

    return {
      total,
      pending,
      processing,
      done,
      overtime
    };
  }
}

// 创建单例实例
const orderDataManager = new OrderDataManager();

// 导出实例
export default orderDataManager; 